package com.edu.service.impl;

import com.edu.component.PageBean;
import com.edu.component.QueryParam;
import com.edu.dao.QuestionMapper;
import com.edu.entity.Question;
import com.edu.entity.QuestionOptions;
import com.edu.service.QuestionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    //1.删除题目
    @Override
    public void deleteQuestion(Integer id) {
        //1.先删除选项
        questionMapper.deleteQuestionOptions(id);
        //2.删除题目
        int cnt = questionMapper.deleteQuestionById(id);
        if ( cnt != 1 ){
            throw new RuntimeException("删除题目失败.");
        }
    }

    //2.更新题目
    @Override
    public void updateQuestion(Question question) {
        question.setStatus(0);   //设置题目为可用的状态
        //1.先更新题目
        int updateCnt = questionMapper.updateQuestion(question);
        int totalCnt = 1;
        //2.如果是选择题，则更新选项
        //typeId: 1(选择题),2(...),3(...)
        Integer typeId = question.getTypeId();
        //3.不管什么题型，去把选项表清理一下
        questionMapper.deleteQuestionOptions(question.getId());
        if (typeId.equals(1)){
            //4.如果当前是选择题，添加选项
            QuestionOptions OPS = question.getQuestionOptions();
            updateCnt += questionMapper.addQuestionOptions(OPS);
            totalCnt += 1;
        }
        //totalCnt: 需要更新记录数，updateCnt: 实际更新的记录数
        if ( updateCnt!=totalCnt){    //如果两个数对不上，就存在失败的操作
            throw new RuntimeException("更新题目失败.");
        }

    }

    //3.添加题目
    @Override
    public void addQuestion(Question question) {
        //1.设置题目状态 0：表示可用
        question.setStatus(0);
        //2.写入数据
        int updateCnt = questionMapper.addQuestion(question);
        int totalCnt = 1;
        //3.如果是选择题，写入选项表
        Integer typeId = question.getTypeId();
        if (typeId.equals(1)){
            QuestionOptions OPS = question.getQuestionOptions();
            //4.设置选项所关联的题目的ID
            OPS.setQuestionId(question.getId());
            //5.保存选择题的选项
            updateCnt += questionMapper.addQuestionOptions(OPS);
            totalCnt = 2;
        }
        if ( updateCnt != totalCnt ){
            throw new RuntimeException("添加题目失败.");
        }
    }

    //4.查询单个题目
    @Override
    public Question queryQuestionByQid(Integer qid) {
        return questionMapper.queryQuestionById(qid);
    }

    @Override
    public PageBean listPage(QueryParam param) {
        //1.开启分页拦截的功能
        // Page: 页码, Limit: 页大小,true: 是否要记录数
        Page retPage = PageHelper.startPage( param.getPage(),param.getLimit(),true);
        //2.执行查询
        questionMapper.queryList( param.getParam() );
        //3.把 Page 装载进 PageBean 并返回
        return new PageBean(retPage);
    }

    @Override
    public void addQuestionOptions(QuestionOptions options) {

    }
}
